package com.example.controller;

import com.example.pojo.Result;
import com.example.utils.AliSMS;
import com.example.utils.CheckUtil;
import com.example.vo.SmsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "短信验证码模块")
@Slf4j
@RestController
@RequestMapping("/api/sms")
public class SMSController {

    @Autowired
    private AliSMS aliSMS;

    @ApiOperation("只有绑定得手机号才能接受验证码")
    @PostMapping
    public Result sms(@RequestBody SmsVO smsVO) throws Exception {
        if (StringUtils.isBlank(smsVO.getPhoneNumber()) || !CheckUtil.isValidPhoneNumber(smsVO.getPhoneNumber())) {
            return Result.error("手机号不正确");
        }
        aliSMS.vc(smsVO.getPhoneNumber());
        return Result.success();
    }

    @ApiOperation("验证码验证")
    @PostMapping("/validate")
    public Result codeValidate(@RequestBody SmsVO smsVO) throws Exception {
        if (StringUtils.isBlank(smsVO.getPhoneNumber()) || !CheckUtil.isValidPhoneNumber(smsVO.getPhoneNumber())) {
            return Result.error("手机号不正确");
        }
        if (StringUtils.isBlank(smsVO.getCode())) {
            return Result.error("验证码无效");
        }
        return aliSMS.codeValidate(smsVO);
    }
}
